package com.xiaoding.serialhelper;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by Administrator on 2017/5/28 0028.
 */

public class TakeSendPopWin extends PopupWindow {
    private Context mContext;
    Activity context;
    private View view;

    private Button btn_send;


    private  void updateUI(int height){

        if(height < 1) {
            // 设置弹出窗体的宽和高
            this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
            this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        }else
        {
            this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT + height);
            this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        }

        // 设置弹出窗体可点击
        this.setFocusable(true);
       // this.update(RelativeLayout.LayoutParams.MATCH_PARENT ,height +2);
    }

    public TakeSendPopWin(final Activity mcontext, View.OnClickListener itemsOnClick) {
        context = mcontext;
        this.view = LayoutInflater.from(mcontext).inflate(R.layout.bottom_menu, null);
        btn_send = (Button) view.findViewById(R.id.send_button);
        // 取消按钮
        btn_send.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // 销毁弹出框
                dismiss();
            }
        });

        // 设置外部可点击
        this.setOutsideTouchable(true);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        this.view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = view.findViewById(R.id.pop_layout).getTop();

                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });


    /* 设置弹出窗口特征 */
        // 设置视图
        this.setContentView(this.view);

        updateUI(0);

        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置弹出窗体的背景
        this.setBackgroundDrawable(dw);

        // 设置弹出窗体显示时的动画，从底部向上弹出
        this.setAnimationStyle(R.style.take_photo_anim);

        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){

            //当键盘弹出隐藏的时候会 调用此方法。
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                //获取当前界面可视部分
                context.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                //获取屏幕的高度
                int screenHeight =  context.getWindow().getDecorView().getRootView().getHeight();
                //此处就是用来获取键盘的高度的， 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
                int heightDifference = screenHeight - r.bottom;
                Log.d("Keyboard Size", "Size: " + heightDifference);

                updateUI(heightDifference);
      //          Toast.makeText(mcontext,"Keyboard Size"+heightDifference,Toast.LENGTH_SHORT).show();
            }

        });

    }
}
